/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import java.util.Calendar;
import java.util.List;
import javax.persistence.*;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author luancorumba
 */
@Entity
public class WorkTopic extends AbstractEntity{
    private static final long serialVersionUID = 1L;
    
    private Integer code;
    
    @Column(nullable=false)
    private String name;
    
    @Column(nullable=false)
    @Temporal(TemporalType.DATE)
    private Calendar initialDate;
    
    @Temporal(TemporalType.DATE)
    private Calendar finalDate;

    @OneToOne
    private WorkTopic replace;
    
    @OneToOne(mappedBy = "replace")
    private WorkTopic replaced;
    
    @ManyToOne
    private WorkTopic father;
    
    @ManyToOne
    private WorkTopic top;
    
    @OneToMany(mappedBy = "father")
    private List<WorkTopic> sons;

    public WorkTopic() {
    }

    @Override
    public void generateHash() {
        if(getId() == null) {
            String actorHashString = ""
                    + this.getName()
                    + CalendarUtil.getFormatedDate(this.getInitialDate())
                    + CalendarUtil.getFormatedDateTimestamp(Calendar.getInstance())
                    + ((this.getFather() != null) ? this.getFather().getId() : "");
            this.setId(Long.valueOf(EncryptionUtil.encrypt(actorHashString).hashCode()));
        }
    }

    public WorkTopic(String name) {
        this.name = name;
    }

    public WorkTopic getFather() {
        return father;
    }

    public void setFather(WorkTopic father) {
        this.father = father;
    }

    public Calendar getFinalDate() {
        return finalDate;
    }

    public void setFinalDate(Calendar finalDate) {
        this.finalDate = finalDate;
    }

    public Calendar getInitialDate() {
        return initialDate;
    }

    public void setInitialDate(Calendar initialDate) {
        this.initialDate = initialDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public WorkTopic getReplace() {
        return replace;
    }

    public void setReplace(WorkTopic replace) {
        this.replace = replace;
    }

    public WorkTopic getReplaced() {
        return replaced;
    }

    public void setReplaced(WorkTopic replaced) {
        this.replaced = replaced;
    }

    public List<WorkTopic> getSons() {
        return sons;
    }

    public void setSons(List<WorkTopic> sons) {
        this.sons = sons;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public WorkTopic getTop() {
        return top;
    }

    public void setTop(WorkTopic top) {
        this.top = top;
    }

    @Override
    public String toString() {
        return "entity.Work[ id=" + this.getId() + " ]";
    }
}
